using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using MM.Entities;

namespace MM.Business
{
    public class clsCHI_TIET_PDH_PYC
    {
        public clsCHI_TIET_PDH_PYC(string sConn)
        {
            SqlHelper.ConnectionString = sConn;
        }
        #region Private methods
        private CHI_TIET_PDH_PYC GetDataRow(DataRow row)
        {
            CHI_TIET_PDH_PYC objCHI_TIET_PDH = new CHI_TIET_PDH_PYC();
            try
            {
                if (row["MaChiTiet"] != DBNull.Value)
                    objCHI_TIET_PDH.MaChiTiet = int.Parse(row["MaChiTiet"].ToString());
            }
            catch { }
           
            try
            {
                if (row["POThamChieuSAP"] != DBNull.Value)
                    objCHI_TIET_PDH.POThamChieuSAP = (row["POThamChieuSAP"].ToString());
            }
            catch { }
            
            
            try
            {
                if (row["MaPYC"] != DBNull.Value)
                    objCHI_TIET_PDH.MaPYC = (row["MaPYC"].ToString());
            }
            catch { }


            return objCHI_TIET_PDH;
        }
        private List<CHI_TIET_PDH_PYC> GetCHI_TIET_PDH(DataTable dt)
        {
            List<CHI_TIET_PDH_PYC> lst = new List<CHI_TIET_PDH_PYC>();
            CHI_TIET_PDH_PYC obj;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                obj = this.GetDataRow(dt.Rows[i]);               
                lst.Add(obj);
            }
            return lst;
        }

        public int usp_InsertCHI_TIET_PDH_PYC(CHI_TIET_PDH_PYC obj)
        {
            int iResult = 0;
            string spName = "usp_InsertCHI_TIET_PDH_PYC";

            try
            {
                SqlParameter[] par = new SqlParameter[2];
                par[0] = new SqlParameter("@POThamChieuSAP", obj.POThamChieuSAP);
                par[1] = new SqlParameter("@MaPYC", obj.MaPYC);
               


                if (SqlHelper.IsHaveTran())
                    iResult = SqlHelper.ExecuteNonQuery(SqlHelper.Tran, spName, par);
                else
                    iResult = SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure, spName, par);

            }
            catch (Exception ex)
            {
            }
            return iResult;
        }

        public List<CHI_TIET_PDH_PYC> DanhsachCHI_TIET_PDH_PYCByPDH(string POThamChieuSAP)
        {
            List<CHI_TIET_PDH_PYC> lst = new List<CHI_TIET_PDH_PYC>();
            string spName = "select * from CHI_TIET_PDH_PYC where POThamChieuSAP='"+POThamChieuSAP+"'";
            DataTable dt = new DataTable();
            try
            {            


                if (SqlHelper.IsHaveTran())
                    dt = SqlHelper.ExecuteToDataTable(SqlHelper.Tran, spName, CommandType.Text);
                else
                    dt = SqlHelper.ExecuteToDataTable( spName,CommandType.Text);
                lst = this.GetCHI_TIET_PDH(dt);
            }
            catch (Exception ex)
            {
            }
            return lst;
        }

        public List<CHI_TIET_PDH_PYC> DanhsachCHI_TIET_PDH_PYCByPYC(string MaPhieuYeuCau)
        {
            List<CHI_TIET_PDH_PYC> lst = new List<CHI_TIET_PDH_PYC>();
            string spName = "select * from CHI_TIET_PDH_PYC where MaPYC='" + MaPhieuYeuCau + "'";
            DataTable dt = new DataTable();
            try
            {


                if (SqlHelper.IsHaveTran())
                    dt = SqlHelper.ExecuteToDataTable(SqlHelper.Tran, spName, CommandType.Text);
                else
                    dt = SqlHelper.ExecuteToDataTable(spName, CommandType.Text);

                lst = this.GetCHI_TIET_PDH(dt);
            }
            catch (Exception ex)
            {
            }
            return lst;
        }

        #endregion Private methods
    }
}
